package com.cloud.clouduser.security.base;

import com.cloud.cloudcommons.utils.CommonCheck;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @Aurher: QiaoHang
 * @Description:
 * @Data: 2019/9/24 14:43
 * @Modified By:
 */
public class SecurityUser implements UserDetails {

    private static  Map<String,String> map;

    static {
        map = new HashMap<>();
        map.put("boolean1","user1");
        map.put("boolean2","user2");
        map.put("boolean3","user3");
        map.put("boolean4","user4");
    }

    //用户名 密码
    private String username;
    private String password;
    private  boolean accountNonExpired;
    private  boolean accountNonLocked;
    private  boolean credentialsNonExpired;
    private  boolean enabled;
    //用户角色
    private List<String> roles;
    //用户的扩展信息
    private Map<String,Object> userExplandInfo;
    //授权角色集合
    private Collection<? extends GrantedAuthority> authorities;

    public SecurityUser() {

    }

    public SecurityUser(String username, String password){
        this.username = username;
        this.password = password;
        this.accountNonExpired = isAccountNonExpired();
        this.accountNonLocked = isAccountNonLocked();
        this.credentialsNonExpired = isCredentialsNonExpired();
        this.enabled = isEnabled();
    }

    public SecurityUser(String username,
                        String password,
                        List<String> roles){
        this.username = username;
        this.password = password;
        this.roles = roles;
        this.accountNonExpired = isAccountNonExpired();
        this.accountNonLocked = isAccountNonLocked();
        this.credentialsNonExpired = isCredentialsNonExpired();
        this.enabled = isEnabled();
    }

    public Map<String, Object> getUserExplandInfo() {
        return userExplandInfo;
    }

    public void setUserExplandInfo(Map<String, Object> userExplandInfo) {
        this.userExplandInfo = userExplandInfo;
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auth = new ArrayList<>();
        try {
            if(CommonCheck.nullList(roles))
                return auth;
            else {
                for (String role : roles) {
                    auth.add(new SimpleGrantedAuthority(role));
                }
            }
            this.authorities = auth;
        }catch (Exception e){
            return auth;
        }
        return auth;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        String boolean1 = map.get("boolean1");
        return boolean1.equalsIgnoreCase(username)?false:true;
    }

    @Override
    public boolean isAccountNonLocked() {
        String boolean2 = map.get("boolean2");
        return boolean2.equalsIgnoreCase(username)?false:true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        String boolean3 = map.get("boolean3");
        return boolean3.equalsIgnoreCase(username)?false:true;
    }

    @Override
    public boolean isEnabled() {
        String boolean4 = map.get("boolean4");
        return boolean4.equalsIgnoreCase(username) ? false : true;
    }
}
